package com.hjj.web.controller;

import com.hjj.web.core.QuartzService;
import com.hjj.web.entity.vo.WebSocketDemoQueryDTO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author: luoyajun
 * @date: 2020/6/1 14:21
 */
@Component
public class TestTopicServiceImp implements Job {

    private static final String JOB_NAME = "testTopicServiceImp";

    @Autowired
    private QuartzService quartzService;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @PostConstruct
    public void init() {
        quartzService.addJob(JOB_NAME, "0/10 * * * * ?", TestTopicServiceImp.class, "测试发布定时任务！");
    }

    @PreDestroy
    public void destroy() {
        quartzService.deleteJob(JOB_NAME);
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        messagingTemplate.convertAndSend("/topic/getResponse", new WebSocketDemoQueryDTO("", "定时任务发布！"));
    }
}
